package com.css.fxfzaqbz.modules.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.AirportData;
import com.css.fxfzaqbz.modules.data.queryParams.AirportDataQueryParams;
import com.css.fxfzaqbz.modules.data.repository.AirportDataRepository;
import com.css.fxfzaqbz.modules.data.service.AirportDataService;
import com.css.fxfzaqbz.util.FileUtil2;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@Service
public class AirportDataServiceImpl implements AirportDataService {
    @Resource
    AirportDataRepository airportDataRepository;

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public AirportData updateAirportData(ActiveFaultData activeFaultData) {
//
//        activeFaultData.setAfdUpdatetime(PlatformDateUtils.getCurrentTimestamp());
//
//        return activeFaultDataRepository.updateActiveFaultData(activeFaultData);
//    }

    @Override
    @Transactional
    public AirportData saveAirportData(AirportData airportData) {
        airportData.setAdId(UUIDGenerator.getUUID());
        airportData.setAdStatus(YNEnum.Y.toString());
        airportData.setAdCreatetime(PlatformDateUtils.getCurrentTimestamp());
        return airportDataRepository.saveAirportData(airportData);
    }

    @Override
    public JSONObject getAllAirportData(AirportDataQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(airportDataRepository.getAllAirportDataParams(queryParams));
    }

    @Override
    public AirportData getAirportDataById(String id) {
        return  airportDataRepository.getAirportDataById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAirportDatas(List<String> ids) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        airportDataRepository.deleteAirportDatasById(ids,timestamp);
    }

    @Override
    public List<AirportData> getAll() {
        return airportDataRepository.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<AirportData> airportData = FileUtil2.importExcel(file, AirportData.class);
        for(int i = 0 ; i<airportData.size();i++){
            airportData.get(i).setAdId(UUIDGenerator.getUUID());
            airportData.get(i).setAdStatus(YNEnum.Y.toString());
            airportData.get(i).setAdCreatetime(PlatformDateUtils.getCurrentTimestamp());
        }
        airportDataRepository.importExcel(airportData);
    }
}
